﻿/**
* 客户字典
**/

function viewModel(data) {
    var self = this;
    this.dataSource = data.dataSource;
    this.grid = {
        size: { w: 239, h: 40 },
        url: "/api/basic/Dwzd",
        queryParams: ko.observable(),
        pagination: true
    };
    this.gridEdit = new com.editGridViewModel(this.grid);
    self.grid.onClickRow = self.gridEdit.ended;
    this.grid.OnAfterCreateEditor = function (edt) {
        com.readOnlyHandler('input')(edt["Dwzd_Dwbh"].target, true);
    };
    this.tree = {
        method: 'GET',
        url: '/api/basic/dwzd/GetTypes',
        queryParams: ko.observable(),
        loadFilter: function (d) {
            var filter = utils.filterProperties(d.rows || d, ['Dwlb_Lbbh as id', 'Dwlb_Lbmc as text', 'Dwlb_Sjbh as pid']);
            var data = utils.toTreeData(filter, 'id', 'pid', "children");
            return [{ id: '', text: '所有类别', children: data }];
        },
        onSelect: function (node) {
            self.CodeType(node.id);
        }
    };

    this.CodeType = ko.observable();
    this.CodeType.subscribe(function (value) {
        self.grid.queryParams({ Dwzd_Lbbh: value });
    });

    this.refreshClick = function () {
        window.location.reload();
    };
    this.addClick = function () {
        var lbbh;
        if (self.CodeType()) {
            lbbh = self.CodeType();
        }
        else {
            return com.message('warning', '请先在左边选择要添加的类别！');
        }
        com.ajax({
            type: 'GET',
            url: '/api/basic/dwzd/getnewcode/' + lbbh,
            success: function (d) {
                var row = { Dwzd_Lbbh: lbbh, Dwzd_Dwbh: d };
                self.gridEdit.addnew(row);
            }
        });
    };
    this.editClick = function () {
        var row = self.grid.datagrid('getSelected');
        if (!row) return com.message('warning', self.resx.noneSelect);
        self.gridEdit.begin()
    };
    this.deleteClick = self.gridEdit.deleterow;
    this.saveClick = function () {
        self.gridEdit.ended();
        var post = { list: self.gridEdit.getChanges() };
        if (self.gridEdit.isChangedAndValid()) {
            com.ajax({
                url: '/api/basic/dwzd/edit',
                data: ko.toJSON(post),
                success: function (d) {
                    com.message('success', '保存成功！');
                    self.gridEdit.accept();
                }
            });
        }
    };
    this.typeClick = function () {
        com.dialog({
        	title: "&nbsp;客户类别",
        	iconCls: 'icon-node_tree',
        	width: 600,
        	height: 440,
        	html: "#type-template",
        	viewModel: function (w) {
        		var that = this;
        		this.grid = {
        			width: 586,
        			height: 360,
        			idField: '_id', //虚拟主键
        			treeField: 'Dwlb_Lbbh',
        			url: "/api/basic/dwzd/gettypes",
        			queryParams: ko.observable(),
        			loadFilter: function (d) {
        				d = utils.copyProperty(d.rows || d, ["Dwlb_Lbbh", "IconClass"], ["_id", "iconCls"], false);
        				return utils.toTreeData(d, '_id', 'Dwlb_Sjbh', "children");
        			}
        		};
        		this.gridEdit = new com.editTreeGridViewModel(this.grid);
        		this.grid.OnAfterCreateEditor = function (editors, row) {
        			if (!row._isnew) com.readOnlyHandler('input')(editors["Dwlb_Lbbh"].target, true);
        		};
        		this.grid.onClickRow = that.gridEdit.begin;
        		this.grid.toolbar = [
                    { text: '新增', iconCls: 'icon-add1', handler: function () { that.gridEdit.addnew(); } }, '-',
                    { text: '删除', iconCls: 'icon-cross', handler: that.gridEdit.deleterow }
        		];
        		this.confirmClick = function () {
        			that.gridEdit.ended();
        			if (that.gridEdit.isChangedAndValid()) {
        			    var list = that.gridEdit.getChanges(['Dwlb_Lbbh', 'Dwlb_Lbmc', 'Dwlb_Sjbh', 'Dwlb_Pxh', 'Dwlb_Remark']);
        				com.ajax({
        					url: '/api/basic/dwzd/edittype/',
        					data: ko.toJSON({ list: list }),
        					success: function (d) {
        						that.cancelClick();
        						self.tree.$element().tree('reload');
        						com.message('success', '保存成功！');
        					}
        				});
        			}
        		};
        		this.cancelClick = function () {
        			w.dialog('close');
        		};
        	}
        });
    };
};